package com.example.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class ResourceLoader {

	private static ResourceLoader _instance = null;
	
	private ResourceLoader(){
		
	}
	
	public static ResourceLoader getInstance(){
		if(_instance ==null){
			_instance = new ResourceLoader();
//			return _instance;
		}
		return _instance;
	}
	
	public InputStream getInputStreamResource(String file, String configLocation) {	
		InputStream is = null;
//		logger.info("Loading file : " + configLocation + file);
		try {
			is = new FileInputStream(configLocation + file);
//			logger.info("Loaded file : " + configLocation + file);
		}
		catch (FileNotFoundException e) {
//			logger.info("NOT FOUND : " + configLocation + file + " - Trying as resource ... ");

			is = getClass().getClassLoader().getResourceAsStream(file);
			if (is != null) {
//				logger.info("Loaded resource : " + file);
			}
			else {
//				logger.info("Loading file : " + file);
				try {
					is = new FileInputStream(file);
//					logger.info("Loaded file : " + file);
				}
				catch (FileNotFoundException ex) {
					//No point trying as resource
//					if (logger.isDebugEnabled()) {
//						logger.debug("File Not Found : " + file, ex);
//					}
				}
			}
		}
		return is;
	}

}
